<script setup lang="ts">
import ebookService from "../http/EbookService";
import TheCommonTable from "../components/the-common-table.vue";
import ebookCategoryService from "../http/EbookCategoryService";
import {EbookCategoryQueryRendered} from "../http/Model";

const columns = [
  {
    title: 'id',
    dataIndex: 'id',
    key: 'id'
  }, {
    title: '名称',
    dataIndex: 'name',
    key: 'name'
  }, {
    title: '分类名称',
    dataIndex: 'categoryName',
    key: 'categoryName'
  }, {
    title: '文档数量',
    dataIndex: 'docCount',
    key: 'docCount'
  }, {
    title: '阅读数',
    dataIndex: 'viewCount',
    key: 'viewCount'
  }, {
    title: '点赞数',
    dataIndex: 'voteCount',
    key: 'voteCount'
  }, {
    title: '操作',
    key: 'action'
  }
]

// 加载分类列表
let categoryList: { label: string, value: string }[] = []
const onTableRefresh = () => {
  ebookCategoryService.findAll().then(({data}) => {
    if (data.success) {
      categoryList.length = 0
      categoryList.push(...data.data.map((category: EbookCategoryQueryRendered) => ({
        label: category.name,
        value: category.code
      })))
    }
  })
}

const editingForm: FormItem[] = [
  {
    label: '名称',
    dataIndex: 'name',
    inputType: 'text'
  }, {
    label: '分类',
    dataIndex: 'category.code',
    formIndex: 'categoryName',
    inputType: 'select',
    options: categoryList
  }, {
    label: '封面',
    dataIndex: 'cover',
    inputType: 'text'
  }, {
    label: '阅读数',
    dataIndex: 'viewCount',
    inputType: 'text'
  }, {
    label: '点赞数',
    dataIndex: 'voteCount',
    inputType: 'text'
  }
]
</script>

<template>
  <the-common-table :service="ebookService"
                    editing-value-key-name="id"
                    :columns="columns"
                    :editing-form="editingForm"
                    @onRefresh="onTableRefresh">
  </the-common-table>
</template>
